package main

import (
	"fmt"
)

func main() {
	/*const name = "hello"
	var a string = "hello"
	var cc = unsafe.Sizeof(name)
	var aa = unsafe.Sizeof(a)
	println(a == name)
	println(aa)
	println(cc)
	var b = 0
	b++
	fmt.Println(b)
	b--
	fmt.Println(b)*/

	//test1()

	//test2()
	//test3()
	//test4("hello")
	/*max := test5(10, 20)
	println(max)*/

	pointerTest()



	/*
	a := 100
	b := 200
	println("交换前的数据a===>",a)
	println("交换前的数据b===>",b)
	swapPointerTest(&a,&b)
	println("交换后的数据a===>",a)
	println("交换后的数据b===>",b)
	*/

	//say(student{"张三",19,"男"})

/*	array(myarray)*/
	//slience()
	//rangeTest("我是中国")
	mymap :=map[string]string{"中国":"北京","日本":"东京"}
	mapTest(mymap)
}

/**
for 循环
*/
func test1() {
	for i := 1; i <= 9; i++ {
		for j := 1; j <= i; j++ {
			print(i, "*", j, "=", i*j, "    ")
		}
		println()
	}
}

// case 语句块
func test2() {
	var expr = 15
	switch expr {
	case 1:
		print(1)
	case 2:
		print(2)
	case 3:
		print(3)
	default:
		print(5)
	}
}

/**
for 循环的变种
*/
func test3() {
	sum := 1
	for sum <= 10 {
		sum += sum
	}
	print(sum)
}

/**
方法参数
 */
func test4(str string) {
	print(str)
}


/**
方法参数
 */
func test5(number1 int, number2 int) int {
	max := 0
	if number1 > number2 {
		max = number1
	} else {
		max = number2
	}
	return max
}

/**
指针类型
 */
func pointerTest(){
	a := 6
 	var 	p *int
	p = &a
	println("变量a 的内存地址为===>",&a)
	println("指针p 的内存地址为===>",p)
	println(*p )
	var n *int
	fmt.Printf("这个是空指针===>%x\n",n)
	if(n == nil){
		print(" n 是空指针")
	}else {
		print(" n 不是空指针")
	}
}

/**
指针类型的数值交换
 */
func swapPointerTest(a *int ,b *int){
	var temp int
	temp = *a
	*a = *b
	*b = temp
}

type student struct {
	name string
	age int8
	sex string
}

func say(stud student)  {
	print(stud.sex)
}

func array(array []int) {
	print(array[0])
}

/**
切片
 */
func slience() {
	// 这是一个数组
	myarray := [...]int{1,2,3,4,5}
	myslice := myarray[0:1]
	println("myarray的切片 myslice为===>",myslice[0])
	println(cap(myslice))
	println(myarray[4])
	println(cap(myarray))
	print(len(myarray))
	myslice = append(myslice, 12)
	fmt.Println(myslice)
	fmt.Println("myslice 的容量为===>",cap(myslice))
	secondSlice := make([]int, len(myslice),(cap(myslice))*2)
	copy(myslice,secondSlice)
	fmt.Println(secondSlice)
	secondSlice[0] = 11
	secondSlice[1] = 12
	fmt.Printf("新切片的容量为===>%d,大小为===>%d,数据为===>%v\n",cap(secondSlice),len(secondSlice),secondSlice)
	for i := 0; i < len(secondSlice); i++ {
		fmt.Println(secondSlice[i])
	}
}

/**
range 函数
 */
func rangeTest(str string) {
	for i,_ :=range str{
		fmt.Println(i)
	}
}


/**
map 测试
 */
func mapTest(myMap map[string]string) {
	for key,value := range myMap{
		fmt.Printf("key===>%s,value ===>%s\n",key,value)
	}
}



